/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/framework/component/InputImpl.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Id: InputImpl.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Author: lanzhouxue $
 * $Date: 2012-07-02 11:09:12 +0800 (Mon, 02 Jul 2012) $
 * $Revision: 1034 $
 */
package com.elancom.framework.component.internal;

import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import com.elancom.framework.component.Input;

/**
 * @author elancom
 * @date Apr 9, 2012
 */
class InputImpl implements Input {
	private static final long serialVersionUID = 4197118779963903810L;

	private boolean isUsed = false;

	private Map<String, Object> parameter = new ConcurrentHashMap<String, Object>();

	InputImpl() {
	}

	@Override
	public Input setParameter(Map<String, Object> paramMap) {
		if (paramMap != null) {
			for (Entry<String, Object> entry : paramMap.entrySet()) {
				paramMap.put(entry.getKey(), entry.getValue());
			}
		}
		return this;
	}

	@Override
	public Input setParameter(String key, Object value) {
		parameter.put(key, value);
		return this;
	}

	@Override
	public boolean containsParameter(String key) {
		return parameter.containsKey(key);
	}

	@Override
	public Boolean getBooleanParameter(String key) {
		Object value = getParameter(key);
		if (value == null) {
			return false;
		}

		if (value instanceof Boolean) {
			return (boolean) value;
		}

		return "true".equals(getTrimString(key));
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T getParameter(String key) {
		return (T) parameter.get(key);
	}

	@Override
	public Map<String, Object> getParameters() {
		return parameter;
	}

	@Override
	public String getTrimString(String key) {
		String str = getParameter(key);
		return str != null ? str.trim() : str;
	}

	public boolean isUsed() {
		return isUsed;
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T removeParameter(String key) {
		return (T) parameter.remove(key);
	}

	public void used() {
		isUsed = true;
	}

	static Input create() {
		return new InputImpl();
	}
}
